<?php
/*
 +-------------------------------------------------------------------+
 |                   browse.func.php                                 |
 |                                                                   |
 | Copyright ?                          www.gde.sclab.clarkson.edu   |
 | Author: Demetrios Dimatos            www.dimatos.net              |
 | Created: August 2006                 Last modified: Oct 15, 2006  |
 +-------------------------------------------------------------------+
 | These functions are used for schema browser                       |
 |  - /gde-1.0/templates/logged-in/browser.php                       |
 |  - /gde-1.0/includes/public/browser.ctl.php                       |
 +-------------------------------------------------------------------+
*/

 // Include configuration variables
 include_once('/var/www/gde-1.0/htdocs/config.php');
 // Include database connnection
 include_once(Config::base_path.Config::database_path."database.php");
  // Include templating for exceptions 
 include_once(Config::base_path.Config::adodb_path.'adodb-exceptions.inc.php');
 
// Start the SESSION vars
if (session_id() == "")
{
	session_start();
}

/**
 * Gets the database description of a database used for browsing the schema
 *
 * @param integer $connection_type - BACKEND or DOMAIN_SPECIFIC
 * @return adodb result set object with database description
 * @author Demetrios Dimatos
 */
function getDatabaseDescription($connection_type)
{
	global $result;
	try 
	{
		$sql = "SELECT databaseID, databaseName, description FROM DatabaseInfo WHERE available >= 1";
  	$result = doQuery($sql,$connection_type);
	}
	catch (Exception $e)
	{
		die( "ERROR executing query ".$e->getMessage());
	}
	
	if($result->RecordCount() == 0)
	{
		return $result = 0;
	}
  return  $result;
}

/**
 * Gets the database name using the database ID
 *
 * @param integer $databaseID - Id of the database
 * @return string - name of the database based on database id
 * @author Demetrios Dimatos
 */
function getDatabaseName($databaseID) 
{
	global $result;
	try
	{
		$query = 'SELECT databaseName FROM DatabaseInfo WHERE databaseID="'. $databaseID.'"';
		$result = doQuery($query,BACKEND);
	}
	catch(Exception $e)
	{
		die( "ERROR executing query ".$e->getMessage());
	}
	
	if($result->RecordCount() == 0)
	{
		return $result = 0;
	}
	return $result->fields[0];
}

/**
 * Gets the tables of a database based on the database id
 *
 * @param integer $databaseID - Id of the database
 * @return adodb resultset object - names of the tables based on database id
 * @author Demetrios Dimatos
 */
function getTableNames($databaseID) 
{
	global $result;

	try
  {
  	$sql = 'SELECT tableName FROM TableInfo WHERE databaseID="'.$databaseID.'"';
		$result = doQuery($sql,BACKEND);
  }
			catch(Exception $e)
	{
		die( "ERROR executing query ".$e->getMessage());
	}
	
	if($result->RecordCount() == 0)
	{
		return $result = 0;
	}
	return $result;
}

/**
 * Gets the table descriptions based on the tablename and description level
 *
 * @param string $tableName - name of table
 * @param integer $descriptionLevel - antiqueted argument,use integer 1
 * @return array - an array of table descriptions
 * @author Demetrios Dimatos
 */
function getTablesDescription($tableName,$descriptionLevel) {
	global $result;
 	$tables_desc = array();
 	for($i=0; $i < $tableName->RecordCount();$i+=1)
 	{
  	try
  	{
  		$sql = "SELECT ProjectElementDescription.description FROM ProjectElementDescription JOIN TableInfo ON TableInfo.tableID = ProjectElementDescription.elementID WHERE TableInfo.tableName = '" . $tableName->fields[0] . "' AND descriptionLevel=$descriptionLevel AND typeID=3";
			$result = doQuery($sql,BACKEND);
  	}
		catch(Exception $e)
		{
			die( "ERROR executing query ".$e->getMessage());
		}
		
		if($result->RecordCount() == 0)
		{
			return $result = 0;
		}
		
		$tables_desc[$i] = $result->fields[0];
		unset($result);
		$tableName->MoveNext();
	}	
	return $tables_desc; // $result;
}

/**
 * Gets a single table description based on the tablename and description level,used after a table is 
 * selected
 *
 * @param string $tableName - name of table
 * @param integer $descriptionLevel - antiqueted argument,use integer 1
 * @return adodb resultset object  - the table description
 * @author Demetrios Dimatos
 */
function getTableDescription($tableName,$descriptionLevel) {
	global $result;
 	try
 	{
 		$sql = "SELECT ProjectElementDescription.description FROM ProjectElementDescription JOIN TableInfo ON TableInfo.tableID = ProjectElementDescription.elementID WHERE TableInfo.tableName = '" . $tableName . "' AND descriptionLevel=$descriptionLevel AND typeID=3";
		$result = doQuery($sql,BACKEND);
 	}
	catch(Exception $e)
	{
		die( "ERROR executing query ".$e->getMessage());
	}
		
	if($result->RecordCount() == 0)
	{
		return $result = 0;
	}
	return $result;
}


?>